package com.shigu.agent.model.impl;

import com.opentae.data.mall.beans.ManagerUser;
import com.opentae.data.mall.beans.MoSpreadByBus;
import com.opentae.data.mall.beans.MoTown;
import com.opentae.data.mall.interfaces.*;
import com.shigu.agent.bo.UserBO;
import com.shigu.agent.exceptions.YeepayException;
import com.shigu.agent.model.MoBusiness;
import com.shigu.passageway.bo.RegistBO;
import com.shigu.passageway.exceptions.PassagewayException;
import com.shigu.route.PassagewayRoute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Date;

@Component
@Scope("prototype")
public class MoBusinessImpl extends MoUserImpl implements MoBusiness{

    @Value("${me.operator_id}")
    public Long defaultAgentId;//默认的代理ID

    private UserBO userinfo;

    @Autowired
    private MoSpreadByBusMapper moSpreadByBusMapper;

    @Autowired
    private MoTownMapper moTownMapper;

    @Autowired
    private PassagewayRoute passagewayRoute;

    public MoBusinessImpl() {
    }

    public MoBusinessImpl(UserBO userinfo) {
        this.userinfo=userinfo;
    }

    public MoBusinessImpl(Long userId) {
        super(userId);
    }


    @PostConstruct
    public void init(){
        if (getUserId() == null&&userinfo!=null) {
            setUserId(createOne(userinfo));
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long createOne(UserBO userinfo) {
        return super.createOne(userinfo);
    }

    @Override
    public void spreadByMe(Long userId) {
        MoSpreadByBus spreadByBus=new MoSpreadByBus();
        spreadByBus.setCreateTime(new Date());
        spreadByBus.setSpreadFrom(getUserId());
        spreadByBus.setSpreadTo(userId);
        moSpreadByBusMapper.insertSelective(spreadByBus);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void realCheck(String idCard, String realName, String cashCard, String bank, String location,Long areaId,String telephone) throws PassagewayException {
        RegistBO bo=new RegistBO();
        bo.setUserId(getUserId());
        MoTown town=moTownMapper.selectByPrimaryKey(areaId);
        bo.setAreaCode(town.getAreaCode());
        bo.setBankAccountNumber(cashCard);
        bo.setBankLocation(location);
        bo.setBankName(bank);
        bo.setIdCard(idCard);
        bo.setRealName(realName);
        bo.setTelephone(telephone);
        passagewayRoute.selwayByPid(1L).regist(bo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCheck(String idCard, String realName, String cashCard, String bank, String location, Long areaId,String telephone) throws PassagewayException {
        RegistBO bo=new RegistBO();
        bo.setUserId(getUserId());
        MoTown town=moTownMapper.selectByPrimaryKey(areaId);
        bo.setAreaCode(town.getAreaCode());
        bo.setBankAccountNumber(cashCard);
        bo.setBankLocation(location);
        bo.setBankName(bank);
        bo.setIdCard(idCard);
        bo.setRealName(realName);
        bo.setTelephone(telephone);
        passagewayRoute.updateAllway(bo);
    }

    @Override
    public Long getUserId() {
        return super.getUserId();
    }

    @Override
    public void setUserId(Long userId) {
        super.setUserId(userId);
    }
}
